package me.numark.fragments;

import android.content.Intent;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.ArrayList;

import butterknife.Bind;
import me.numark.R;
import me.numark.activities.LogInActivity;
import me.numark.net.BaseResponse;
import me.numark.utils.Global;

/**
 * Created by Evan on 15/7/24.
 */
public abstract class BaseListFragment extends BaseFragment implements UltimateRecyclerView.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    @Bind(R.id.update_msg)
    @Nullable
    TextView updateMsg;

    @Bind(R.id.recycler_view)
    UltimateRecyclerView recyclerView;

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    long firstClickTime = 0;
    long secondClickTime = 0;

    int empty_text = R.string.default_empty_text;

    UltimateViewAdapter mAdapter;
    ArrayList dataList;
    long lastResponseTime;
    View loadMoreView = null;

    public String searchKey;

    @Override
    public void initUI(View view) {
        super.initUI(view);
        recyclerView.enableLoadmore();
        recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
        recyclerView.setOnLoadMoreListener(this);
        recyclerView.setDefaultOnRefreshListener(this);
        recyclerView.mSwipeRefreshLayout.setColorSchemeResources(R.color.btn_green);
//        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        recyclerView.setAdapter(mAdapter);
        loadMoreView = LayoutInflater.from(getActivity())
                .inflate(R.layout.custom_bottom_progressbar, recyclerView, false);
        recyclerView.enableLoadmore();
        mAdapter.setCustomLoadMoreView(loadMoreView);
        recyclerView.disableLoadmore();
        showEmptyView();
        toolbar.setOnClickListener(this);
    }

    public void enableRefresh(boolean value) {
        recyclerView.mSwipeRefreshLayout.setEnabled(value);
    }

    @Override
    public void loadMore(int itemsCount, final int maxLastVisiblePosition) {

    }

    @Override
    public void onRefresh() {

    }

    public void setHasMore(boolean hasMore) {
        if (hasMore) {
            recyclerView.reenableLoadmore(loadMoreView);
        } else {
            recyclerView.disableLoadmore();
        }
    }

    @Override
    public void refresh() {
        super.refresh();
        onRefresh();
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                recyclerView.setRefreshing(true);
            }
        });
    }

    public void showEmptyView() {
        recyclerView.showEmptyView();
        TextView emptyText = (TextView) recyclerView.findViewById(R.id.empty_text);
        if (needLogin && !Global.isLoggedIn()) {
            SpannableString str = new SpannableString(getString(R.string.empty_login_text));
            str.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    startActivity(new Intent(getActivity(), LogInActivity.class));
                }
            }, 1, 3, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            emptyText.setText(str, TextView.BufferType.SPANNABLE);
            emptyText.setMovementMethod(LinkMovementMethod.getInstance());
        } else {
            emptyText.setText(empty_text);
        }
    }

    public void endRefreshing() {
        recyclerView.setRefreshing(false);
    }

    @Override
    protected void bindData(BaseResponse response, boolean isNew) {
        super.bindData(response, isNew);
        if (response.ret) {
//            lastResponseTime = response.timestamp;

            addDataToList(response, isNew);
            mAdapter.notifyDataSetChanged();
        }
        if (mAdapter.getAdapterItemCount() == 0) {
            showEmptyView();
        } else {
            recyclerView.hideEmptyView();
        }
    }

    protected abstract void addDataToList(BaseResponse response, boolean isNew);

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar: {
                secondClickTime = SystemClock.uptimeMillis();
                if (secondClickTime - firstClickTime < 300) {
                    if (dataList != null && dataList.size() > 0)
                        recyclerView.scrollVerticallyToPosition(0);
                    firstClickTime = 0;
                } else {
                    firstClickTime = secondClickTime;
                }
            }
            break;
        }
    }
}
